package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.Utils.TimeUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.FKnowInfoActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.UpdateCommentActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.AllCommentKnowBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.DeleteKnowCommentModel;

public class UserFKnowAllCommentAdapter extends BaseAdapter<AllCommentKnowBean> {
    private  String times;

    public UserFKnowAllCommentAdapter(Context context, List<AllCommentKnowBean> items, int layoutResourse) {
        super(context, items, layoutResourse);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        UserFKnowAllCommentViewHolder viewHolder = new UserFKnowAllCommentViewHolder(itemView);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final UserFKnowAllCommentViewHolder viewHolder=(UserFKnowAllCommentViewHolder) holder;
        final AllCommentKnowBean allCommentKnowBean=items.get(position);

        final String title = allCommentKnowBean.getFknow_title();
        final String content = allCommentKnowBean.getFkc_content();
        final String time = allCommentKnowBean.getFkc_updatetime();
        final String fkcid = allCommentKnowBean.getFkc_id();
        final  String fknow_id = allCommentKnowBean.getFknow_id();

        viewHolder.allcommentknow_title.setText(title+"------>");
        viewHolder.allcommentknow_content.setText(content);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
             times = TimeUtils.getTimeFormatText(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        viewHolder.allcommentknow_time.setText(times);

        viewHolder.allcommentknow_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentu = new Intent(context, UpdateCommentActivity.class);
                intentu.putExtra("fkcid",fkcid);
                intentu.putExtra("fkccontent",content);
                context.startActivity(intentu);

            }
        });
        viewHolder.allcommentknow_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, FKnowInfoActivity.class);
                intent.putExtra("fknow_id",fknow_id);
                //intent.putExtra("fkc_content",content);
                context.startActivity(intent);
            }
        });




        viewHolder.allcommentknow_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog dlg = new AlertDialog.Builder(context)//builder创建对话框
                        .setIcon(R.mipmap.kpl)//显示头像
                        .setTitle("温馨提示")//显示标题
                        .setMessage("你确定要删除评论吗")//显示类容
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                DeleteKnowCommentModel deleteKnowCommentModel = new DeleteKnowCommentModel();
                                deleteKnowCommentModel.deleteKnowComment(fkcid, new TListener<SuccessBean>() {
                                    @Override
                                    public void onResponse(SuccessBean successBean) {
                                        if (successBean.getSuccess().equals("1")){
                                            Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
                                            items.remove(position);
                                            notifyItemRemoved(position);
                                            notifyItemRangeChanged(0,items.size());
                                            Message message = Message.obtain();
                                        }
                                        else{
                                            Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onFail(String msg) {

                                    }
                                });

                            }
                        })//按钮，null可以选择点击后要执行的事情
                        .create();//创建对话框
                dlg.show();





            }
        });


    }



}
